package com.slowly.intercepto;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
/**
 * 针对管理后台的异常拦截处理类
 * 
 * @author xuping
 *
 */
public class ExceptionInterceptor implements Interceptor {
	private static Logger log = LoggerFactory.getLogger(ExceptionInterceptor.class);
	@Override
	public void intercept(Invocation inv) {
		if (inv.getActionKey().startsWith("/admin")) {
			try {
				inv.invoke();
			} catch (Throwable e) {
				log.error("action error URL:" + inv.getActionKey(), e);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("success", false);
				if (e instanceof NullPointerException) {
					map.put("msg", "操作出错:请联系管理员...NOP..");
				} else {
					map.put("msg", e.getMessage());
				}
				inv.getController().renderJson(map);
			}
		} else {
			inv.invoke();
		}
	}
}
